<?php
/* 
 * Refresh all cams.
 */
header('Content-Type: text/xml; charset=UTF-8');
require_once("PlistParser.inc");
require_once("../../config.php");
require_once("myLib.php");
global $CFG, $COURSE, $USER;

//Paramètres
$Ginstid = optional_param('instid', null, PARAM_INT);
$Gcid = optional_param('cid', null, PARAM_SAFEDIR);

$doc = new DOMDocument('1.0', 'utf-8');
$r = $doc->createElement( "message" );
$doc->appendChild( $r );
$success = "SUCCESS";
$reason = "";
$update = false;

if(isset($Ginstid)){
    if(is_allowed('mod/podcast:edit', $COURSE->id, $Ginstid)){
        $parser = new plistParser();
        //On se renseigne auprès du serveur podcast pour connaitre l'état des caméras
        $command1 = "ssh -i ".$CFG->podcast_path2rsakey." ".$CFG->podcast_username."@".$CFG->podcast_path2pcp." pcast --server ".$CFG->podcast_path2pcp." --user ".$CFG->podcast_username." --pass ".$CFG->podcast_password." --listcameras";
        $output =  shell_exec($command1);
        $parser = new plistParser();
        $plist = $parser->parseRAW($output);

        //On se renseigne auprès de la base de données pour savoir si les caméras sont vérouillées ou non.
        $verrous =  get_records("podcast_cameras");
        /*if ($verrous == null){
            $reason = "BDD error";//"BDD error";
        }else{*/
            $i = 0;
            if(isset($Gcid)){
                foreach($plist["cameras"] as $camera) {

                    $parsers[$i] = new plistParser();

                    if(file_exists($CFG->dataroot."/".$Gcid."/podcast/".$camera['uuid'].".png".floor(time()/600))){
                        $command1 = "ssh -i ".$CFG->podcast_path2rsakey." ".$CFG->podcast_username."@".$CFG->podcast_path2pcp." pcast --server ".$CFG->podcast_path2pcp." --user ".$CFG->podcast_username." --pass ".$CFG->podcast_password." --status \\'".$camera['name']."\\'";
                    }else{
                        $update = true;
                        $command1 = "ssh -i ".$CFG->podcast_path2rsakey." ".$CFG->podcast_username."@".$CFG->podcast_path2pcp." pcast --server ".$CFG->podcast_path2pcp." --user ".$CFG->podcast_username." --pass ".$CFG->podcast_password." --status \\'".$camera['name']."\\' --update_preview";
                    }
                    $output = shell_exec($command1);
                    if($update){
                        if (!is_dir($CFG->dataroot."/".$Gcid."/podcast")){
                            mkdir($CFG->dataroot."/".$Gcid."/podcast", 0777);
                        }
                        shell_exec("rm ".$CFG->dataroot."/".$Gcid."/podcast/".$camera['uuid']."*");
                        shell_exec("curl -m 5 https://".$CFG->podcast_path2pcp.":8170/podcastproducer/previews/fetch?uuid=".$camera['uuid']." -k -u ".$CFG->podcast_username.":".$CFG->podcast_password." -o ".$CFG->dataroot."/".$Gcid."/podcast/".$camera['uuid'].".png".floor(time()/600));
                    }
                    $plists[$i] = $parser->parseRAW($output);
                    $c = $doc->createElement( "camera" );
                    $r->appendChild( $c );
                    $name = $doc->createElement( "name" );
                    $name->appendChild( $doc->createTextNode( $camera['name'] ));
                    $c->appendChild($name);
                    $uuid = $doc->createElement( "uuid" );
                    $uuid->appendChild( $doc->createTextNode( $camera['uuid'] ));
                    $c->appendChild($uuid);
                    $cs = $doc->createElement( "recording_status" );
                    $c->appendChild($cs);
                    $cs->appendChild( $doc->createTextNode( $plists[$i]['recording_status']) );
                    $css = $doc->createElement( "started_at" );
                    $css->appendChild( $doc->createTextNode( $plists[$i]['stopped_at']) );
                    $c->appendChild($css);
                    $csst = $doc->createElement( "stopped_at" );
                    $csst->appendChild( $doc->createTextNode( $plists[$i]['stopped_at']) );
                    $c->appendChild($csst);
                    $ce = $doc->createElement( "elapsed" );
                    $ce->appendChild( $doc->createTextNode( $plists[$i]['elapsed']) );
                    $c->appendChild($ce);
                    $verr = $doc->createElement( "verrou" );
                    $tmp = $doc->createTextNode( "UNLOCKED" );
                    if(!empty($verrous)){
                        foreach ($verrous as $verrou ) {
                            if ($verrou->uuid === $camera['uuid']){
                                $tmp = $doc->createTextNode( "LOCKED" );
                                $verr->setAttribute("user", $verrou->user);
                            }
                        }
                    }
                    $verr->appendChild( $tmp );
                    $c->appendChild($verr);
                    $i++;
                }
            }
        //}
    }else{
        $reason = 'Permission denied';
        $success = "FAILURE";
    }
}else{
    $reason = 'Permission denied';
    $success = "FAILURE";
}
$xmlRespSucc = $doc->createElement( "success" );
$xmlRespSuccWhy = $doc->createElement( "reason" );
$xmlRespSucc->appendChild( $doc->createTextNode( $success ));
$xmlRespSuccWhy->appendChild( $doc->createTextNode( $reason ));
$r->appendChild( $xmlRespSucc );
$r->appendChild( $xmlRespSuccWhy );
echo $doc->saveXML();
?>
